package cn.aitrox.ry.app.web.interceptor;

import cn.aitrox.ry.common.interceptor.UserThreadLocal;
import cn.aitrox.ry.common.service.api.dto.User;
import cn.aitrox.ry.util.JSONUtil;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ApiFeignRequestInterceptor implements RequestInterceptor {

    private static final String USER = "User";

    @Autowired
    private UserThreadLocal userThreadLocal;

    @Override
    public void apply(RequestTemplate template) {
        User user = userThreadLocal.getUser();
        if(null == user) {
            return;
        }
        String userJson = JSONUtil.toJson(user, User.class);
        template.header(USER, userJson);
    }
}
